/**
 * @version Create on 2012-11-1
 * @author Yinzi Chen
 */

public class DistinctSubsequences {

	public int numDistinct(String S, String T) {
		int n, m;
		n = S.length();
		m = T.length();
		if (m == 0 || n < m)
			return 0;
		int[][] d = new int[m + 1][n + 1];
		for (int i = 0; i <= n; ++i)
			d[0][i] = 1;
		for (int i = 1; i <= m; ++i) {
			for (int j = 1; j <= n; ++j) {
				d[i][j] += d[i][j - 1];
				if (T.charAt(i - 1) == S.charAt(j - 1)) {
					d[i][j] += d[i - 1][j - 1];
				}
			}
		}
		return d[m][n];
	}

	public static void main(String[] args) {

	}

}
